<?                                                           
/***************************************************************************                                             
 *                              process.php                                             
 *                           --------------------                                             
 *   begin                : Friday, Nov 04, 2005                                             
 *   copyright            : (C) 2005 cyber8 netWorks                                             
 *   email                : support@cyber8.net                                              
 *                                             
 * # for processing binary requests from news servers                                             
 ***************************************************************************/                                             
$pp_v  = $_GET["pp_v"];                                             
$pp_x  = $_GET["pp_x"];                                             
$pp_y  = $_GET["pp_y"];                                             
$pp_w  = $_GET["pp_w"];                                             
$pp_s  = $_GET["pp_s"];                                             
$pp_a  = $_GET["pp_a"];                                             
$redir = $_GET["redir"];                                             
$resize_limit = 0;                                             
$resize_font  = NULL;                                             

#BEGIN group image processing                                             
if ($_GET["gp"]!="")                                             
{                                                
    $breadcrumb .= "Pb";                                             
    $gr=preg_replace (                                             
                      array("/alt\./","/\.binaries/","/\.multimedia/","/\.pictures/","/\.nospam/","/\.erotic[a]*/","/\.nonude/"),                                             
                      array("a.",".b",".m",".p",".ns",".e",".nn"),                                             
                      get_request("art_group")                                             
                     );                                             
    $resize_limit = $pp_w;                                             
    $resize_font  = array (                                             
                       "color"=>array(                                             
                                      "red"=>9,                                             
                                      "green"=>99,                                             
                                      "blue"=>9                                                 
                                     ),                                             
                       "size"=>$pp_s,                                             
                       "x"=>$pp_x,                                             
                       "y"=>$pp_y,                                             
                       "angle"=>$pp_a,                                             
                       "file"=>DEFAULT_IMAGE_FONT,                                             
                       "text"=>$gr                                             
                       );                                             
                                                 
                                                 
                                                 
    if ($m=="Z")                                             
    {                                             
        $resize_font["text"] = "";                                             
    }                                             
    else if ($m!="X")                                             
    { # if this is an article request, draw the article and stop here                                             
        decode_message_content_output(array(), $resize_limit, $resize_font, $m, get_request("art_group"));                                             
        exit;                                             
    }                                             
                                                 
    $list_array = false;                                             
                                                 
    #BEGIN: select random images from a newsgroup to display                                             
    # if there is a cachefile of articles, use that                                             
    $cache_name = base64_encode("preview_source__" . $nntp_server . "." . $gr . ".dat");                                             
    $cache_path = "data/temp/idx/" . $cache_name;                                               
    $last_article = $_GET["gp"];                                             
    if (file_exists($cache_path))                                             
    {                                             
        $breadcrumb .= "Pc";                                             
        $content = implode ('', file ($cache_path));                                             
        $timestamp = filemtime($cache_path);                                             
        parse_str ($content);                                                      
        if (time() - $timestamp < CACHE_AGE_LIMIT)                                             
        {                                              
            $r = time() % count ($arr);                                             
            $m = $arr[$r]; /* set the MESSAGE_ID to a random image */                                               
            $raw_cache_file = cache_file ($m, "raw", get_request("art_group"));                                              
            if (file_exists($raw_cache_file))                                             
            { # if it has already been cached, stop and display it now                                            
                $breadcrumb .= "Pd";                                             
                $breadcrumb .= "[$raw_cache_file]";                                             
                decode_message_content_output(array(), $resize_limit, $resize_font, $m, get_request("art_group"));                                             
                exit;                                             
            }                                             
        }                                              
        else                                             
        {                           
            $breadcrumb .= "[$timestamp]";                                        
            $last_article = array_pop ($arr);                                             
            unlink ($cache_path);                                             
        }                                             
    }             
                           
    $breadcrumb .= "Px";                        
    check_asset_cache ($m, get_request("art_group"), $pp_w);         
    #if (!file_exists($cache_path) || $m=="X")             
    #{                                            
    #find the first listed  server that has this group                                             
    $nntp = group_lookup ($user, $pass, get_request("art_group"));                                                      
     $breadcrumb .= "[$m]"; # "[".$nntp_server."]";                                                         
                                                 
                                                   
    if (!$nntp)                                              
    {                                              
        header("Content-Error: " . $nntp->get_error_message());                                             
        echo "<b>".$messages_ini["error"]["nntp_fail"]."</b><br>";                                              
        echo $nntp->get_error_message()."<br>";                                              
    }                                              
    else # if ($m=="X") if there is no cache file, them message_id will still be "X"                                            
    {                                             
        $jpgs = $nntp->search_newsgroup($last_article, "*.jpg*", "subject", 1);                                             
        $list_array = $jpgs["indices"];                                                           
        $arr="";                                              
        if (count ($list_array) > 0)                                             
        {                                             
            $listx=0;                                             
            $r = time() % count ($list_array);                                             
            $m = "";                                             
            $breadcrumb .= "Pe";                                             
            foreach ($list_array as $idc)                                             
            {                                             
                $arr .= ($arr==""?"":"&") . "arr[" . $listx . "]=" . $idc;                                        
                if ($listx==$r) $m = $idc;                                           
                $listx++;                                               
            }                                             
            $arr .= "&timestamp=" . time();                                                     
            WriteToFile($cache_path, $arr);                                                         
            reset ($list_array);                                           
            $arr="";                                               
            if ($pp_x=="")                                             
            {                                              
                $resize_limit = 200;                                             
            }                                          
        }                                             
        else                                             
        {                                             
            print "Could not find an image";                                           
            exit;                                       
        }                                                 
        $jpgs = NULL;                                             
    }                                             
    $nntp->quit();                 
    #}                                            
}                                              
#END group image processing                                             
else if ($pp_w!="")                                             
{                                             
    $resize_limit = $pp_w;                                             
}                                              
                    
     
if ($action=="search")                                                                                                                                                       
{                                                                                                                                                       
    include ("addon/search.php");                                                                                                                                                       
}                                        

#BEGIN normal binary processing                                                                                          
if ($m!="")                                              
{                                                     
    if ($action=="decode")                                
    {                                
        include ("addon/save_handler.php");                                
        exit;                                
    }    
    else if ($action=="build")                                
    {                                
        $pointer_name = CACHE_PATH . "pointer/" . base64_encode ($m);         
        if (file_exists($pointer_name))       
        {        
            $fx=fopen ($pointer_name, "r");           
            $fr=fread($fx, filesize($pointer_name));                     
            fclose ($fx);              
            $save_name = CACHE_PATH . $fr;      
            if (file_exists($save_name))      
            {      
                echo "Existing:" . $save_name;      
                exit;      
            }        
        }                               
    }    
                                                  
    $breadcrumb .= "Pf\n";                                             
    #first check to see if this image file is cached and                                             
    #that no other action is being requested. if so draw the image          
    #and stop here                                                            
    if ($action==""&&$info=="")                                             
    {           
        check_asset_cache ($m, get_request("art_group"), $pp_w);                                             
    }                                             
                                     
                                                
    # find the first server that contains this group                                             
    # TO DO: user should be able to name a default server that                                             
    #        would become the first server that is looked at                                                     
    $the_group = get_request("art_group");                                                                    
    # $nntp = group_lookup ($user, $pass, $the_group, $rep_server);                                                     
    if (strlen( get_request("username") ) == 0) $nntp = group_lookup ($user, $pass, $the_group);                                                     
	else $nntp = NULL;

    $breadcrumb .= "[$nntp]\n";                             
    if (!$nntp)     
	{
        $breadcrumb .= "\n***opening alternate***\n";   
	    $user=get_request("username");
		$pass=get_request("password");
		$nntp_server=get_request("nntp");
    	$nntp = new NNTP(get_request("nntp"), $user, $pass);                                        
        if (!$nntp->connect())                                                  
        {                                                  
                                                
            header("Content-Error: " . $nntp->get_error_message());                                                 
            echo $nntp_server."!<b>".$messages_ini["error"]["nntp_fail"]."</b><br>";                                                  
            echo "Error: " . $nntp->get_error_message()."<br>";    
			exit;                                              
        }   
		
		if ( ! ( $gi=$nntp->join_group(trim($the_group)) ) )                    
		{
		    echo "\nUnable to join group [$the_group] on $nntp_server ($user, $pass)";  
			exit;
		}           
	}  
                                      
    if (!$nntp)                                              
    { # STOP processing if there is a problem connecting      
		echo 'Unable to create server ' . get_request("nntp");                                       
//        header("Content-Error: " . $nntp->get_error_message());                                             
//        echo "<b>".$messages_ini["error"]["nntp_fail"]."</b><br>";                                              
//        echo $nntp->get_error_message().$nntp_server."<br>";                                              
        exit;                                             
    }                                              
    else                                              
    {    
	                                                       
        if ($action=="slide")                                              
        {                                              
            include ("addon/slides.php");                                              
        }                                              
        else if ($action=="form")                                              
        {                                                
            include ("addon/testform.php");                                              
        }                                             
        else if ($action=="mnzb")                                              
        { # UNKNONW::LEGACY                                             
            $arr = explode (',', $m);                                              
            $MIME_Message = $nntp->get_header($arr[0]);                                                
            $header = $MIME_Message->get_main_header();                                               
            $nzb =gnzb_create($nntp, $MIME_Message, $arr);                                              
            print $nzb;                                             
            exit;                                              
        }                                              
        else                                              
        {                                                
            if ($action=="save" || $action=="nview" || $action=="eview" || $action=="decode" || $action=="build")                                              
            {  # REMOTE REQUEST handler for file-building                                             
                 include ("addon/save_handler.php");                                              
            }                                             
            else if ($MIME_Message = $nntp->get_header(trim($m)))                                             
            {  #look up the message header here and...                                             
                $header = $MIME_Message->get_main_header();                                                
            }                                              
            else                                             
            {  #...if the message is not found then throw an error                                                             
//                print_r ($_GET);        
//				echo $nntp->get_error_message();      
//				echo "\nUnable to read message [$m] on [$nntp_server] in [$the_group]!!!";  
//				exit;                                                                    
                $breadcrumb .= "\nUnable to read message [$m] on [$nntp_server] in [$the_group]!!!";                                             
                print_r ($_GET);              
				echo '<xmp>'.$breadcrumb.'</xmp>'; //draw_gd_image (false, NULL, 100);   
				exit;                                          
            }                                                   
                                                           
                                                
                                                                               
            if ($action=="header")                                              
            { # newsgroup list for individual articles, when requested                                                
                $newsgroups = explode (',', $header["newsgroups"]);                                          
                $xml='<table width="300">';       
				while (list($x,$y)=each($header))
				{
					if ($x=='from') $xml.='<tr><td align="right"><strong>'.$x.'</strong></td><td>'.$y['name'].'</td></tr>';
					else if ($x=='newsgroups') 
					{                                              
						$xml.= "<tr><td colspan='2'><strong>newsgroups</strong>:<br>";         
						foreach ($newsgroups as $newsgroup)                                             
						{                                             
							$xml.= " - $newsgroup<br>";                                             
						}    
						$xml.= "</td></tr>";                      
					}
					else $xml.='<tr><td align="right"><strong>'.$x.'</strong></td><td><div style="width:200px;overflow:hidden">'.htmlentities($y).'</div></td></tr>';//$xml.=$x.': '.htmlentities($y).'<br>';
				}                                          
                $xml.='</table>';               
				echo $xml;
				exit;                                                           
                foreach ($newsgroups as $newsgroup)                                             
                {                                             
                    print "<a href='" . $href . "?g=" . $newsgroup . "'>" . $newsgroup . "</a><br>";                                             
                }                                                     
                exit;                                             
            }                                             
            else if ($action=="snzb")                                              
            { # UNKNONW::LEGACY                                             
                $gnzb = multipart_render ($nntp, $MIME_Message, "yes", false);                                             
                print_r ($gnzb);                                             
                exit;                                             
            }                                             
            else if ($action=="pnzb")                                              
            { # UNKNONW::LEGACY                                             
                $gnzb = multipart_render ($nntp, $MIME_Message, "", true);                                             
                print_r ($gnzb);                                             
                exit;                                             
            }                                             
            else if ($action=="hnzb")                                              
            { # UNKNONW::LEGACY                                             
                $cancel_event = true;                                              
                $make_param = preg_replace ("/([\s|\d|\]|\[|_])/", "*", $header["subject"]);                                                 
                $list = $nntp->search_newsgroup (1, $make_param . "*", "subject", false, false, true);                                             
                $list = $list["articles"];                                                
                print "<?" . "xml version='1.0'?" . ">\n";                                             
                print "<xml>";                                             
                foreach ($list as $item)                                             
                {                                             
                    $ok=true;                                             
                    $reg_tmp = preg_match("/[(](\d+)\/(\d+)[)]/", $item->subject, $tmp);                                              
                    if ($reg_tmp&&$tmp[1]!=1)                                             
                    {                                              
                        $ok=false;                                             
                    }                                             
                    if ($ok)                                             
                    {                                             
                        print "<file subject=\"" . htmlentities(trim($item->subject)) . "\" />";                                              
                    }                                             
                }                                              
                print "</xml>";                                             
                exit;                                               
            }                                             
            else if ($action=="gnzb")                                              
            {  # LEGACY::MULTI-FILE NZB CREATE action                                             
                preg_match("/([\-a-zA-Z()\s\d]*\.\w{3})/", $header["subject"], $tmp);                                              
                print "<form action='".$href."?".$qs."' method='post'>";                                                             
                $gnzb = gnzb_create($nntp, $MIME_Message);                                             
                print "<input type='hidden' name='dlname' value='msgid_".$m."_".$tmp[1].".nzb'><textarea style='display:none' name='dlnzb'>";                                              
                print htmlentities( $gnzb );                                             
                print "</textarea>";                                             
                print "</form>";                                             
                exit;                                              
            }                                               
            else if ($action=="nzb")                                              
            {  # NZB CREATE action                                             
                header('Content-Disposition: attachment; filename="msgid_'.$m.'.nzb"');                                               
                $nzb = nzb_create($nntp, $MIME_Message);                                              
                print $nzb;                                             
                exit;                                              
            }                                              
            else if (strlen($info)>0)                                              
            {                                              
                include ("addon/info_handler.php");                                              
            }                                              
                                                          
                                                          
            /*                                             
            From this part onward deal with actual image creation, assuming no                                             
            other action is going to be requested                                                         
            */                                              
                                                          
                                                          
            $breadcrumb .= "[".$header["subject"]."]";                                             
            #check the subject line to see if this is a multipart file                                             
            $multi_test = multipart_lookup ($header["subject"], $nntp, true);                                                         
            if ($multi_test)                                             
            { # if so, render the image and stop here                                             
                $breadcrumb .= "Pg";                                                             
                $nzb = multipart_render($nntp, $MIME_Message, "", false, $resize_limit, $resize_font);                                             
            }                                             
                                                         
            # pull the article body here                                             
            if (! $MIME_Message = $nntp->get_article($m))                                             
            { # throw an error if the article body is not found                                             
                $breadcrumb .= "Unable to read message [$m] on [$nntp_server] in [$group]";                                             
                draw_gd_image (false, NULL, 100);                                             
                exit;                                             
            }                                                
                                                         
            $nntp->quit();    // Quit sooner to release the resources                                                 
                                                         
                                                         
            $header = $MIME_Message->get_main_header();                                               
            $parts  = $MIME_Message->get_all_parts();                                                 
            $count  = 0;                                                

            $breadcrumb .= "Ph";                                             
            foreach ($parts as $part)                                              
            {                                              
                $subject = $part["header"]["subject"];                                                    
                $body    = $part["body"];                                              
                if ( (strpos($body, "ybegin") !== false) || (strpos(strtolower($subject), "yenc") !== false) )                                             
                { // YENC decoding                                             
                    $arr = array ();                                             
                    $arr["header"]     = array ("content-type"=>"image/jpeg", "content-transfer-encoding" => "yenc");                                             
                    $arr["body"]       = $part["body"];                                              
                    $breadcrumb .= "Pi";                      
            		#header ("Content-disposition: attachment; filename=$subject");                         
                    decode_message_content_output($arr, $resize_limit, $resize_font, $m, $the_group);                                              
                    exit;                                             
                }                                               
                $attachment_id = $count;                                              
                $content_page  = "webnews/attachment_handler.php";                                              
                $count++;                                              
            }                                               
            header ("Content-disposition: attachment; filename=$subject");                                          
            $breadcrumb .= "Pj";                                             
            if ($MIME_Message->get_total_part() > $attachment_id)                                              
            {                                             
                $header = $MIME_Message->get_part_header($attachment_id);                                             
                $body   = $MIME_Message->get_part_body($attachment_id);                                             
                if (strcmp($header["content-type"],"") == 0)                                              
                {                                             
                    header("Content-Type: text/html");                                             
                    echo $messages_ini["error"]["request_fail"];                                             
                }                                              
                else                                              
                {                                             
                    ob_end_clean();                                             
                    $pos = strpos($header["content-type"], ";");                                             
                    if ($pos !== FALSE)                                              
                    {                                             
                        $header["content-type"] = substr($header["content-type"], 0, $pos);                                             
                    }                                              
                                                                 
                    header("Content-Type: ". $header["content-type"]);                                              
                #    header("Content-Disposition: ".$header["content-disposition"]);      
					header ("Content-disposition: attachment; filename=fname.jpg");                                       
                    $breadcrumb .= "[Saving as " . $image_cache_file . "]";                                             
                    decode_message_content_output($MIME_Message->get_part($attachment_id), $resize_limit, $resize_font, $m, $the_group);                                             
                    exit(0);                                             
                }                                             
            }                                              
            else                                              
            {                                             
                header("Content-Type: text/html");                                             
                echo $messages_ini["error"]["multipart_fail"];                                             
            }                                             
            exit;                                                                                                                                                
        }                                              
    }                                              
    $nntp->quit();                                                                                                                                              
    exit;                                                                                                                                                    
}                                                   
?>